//
//  CourseMainCollectionViewController.m
//  Arts1
//
//  Created by 孤星晨宇 on 15/9/20.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "CourseMainCollectionViewController.h"
#import "CourseMainCollectionViewCell.h"
#import "AllHeader.h"
#import "CourseModel.h"

//  视频列表接口
#define clURL @"http://arts1.dlab.com.cn/app/course"

@interface CourseMainCollectionViewController ()


// 数据源
@property (nonatomic, strong) NSMutableArray *dataArr;


@end

@implementation CourseMainCollectionViewController

static NSString * const reuseIdentifier = @"maincollectioncell";

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // self.clearsSelectionOnViewWillAppear = NO;
    
    self.collectionView.backgroundColor = kBackgroundColor;
    self.collectionView.alwaysBounceVertical = YES;
    
    [self.collectionView registerClass:[CourseMainCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self requestData];
    // Do any additional setup after loading the view.
}

- (void)setCatId:(NSString *)catId{
    _catId = catId;
    [self requestData];
}

- (void)requestData{
        AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
        manage.responseSerializer.acceptableContentTypes  =[NSSet setWithObject:@"text/html"];
        manage.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        //请求参数
      NSDictionary *pardic = nil;
    if (_catId && ![_catId isEqualToString:@"推荐"]) {
        pardic = @{@"catId":_catId};
    }
        [manage POST:clURL parameters:pardic
             success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             // 请求成功的时候调用这个block
             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             //返回的状态
             int index = [dic[@"status"] intValue];
             if (index == 1) {
                 _dataArr = nil;
                 NSArray *arr = dic[@"list"];
                 for (NSDictionary *dict in arr) {
                     CourseModel *course = [[CourseModel alloc] init];
                     [course setValuesForKeysWithDictionary:dict];
                     [self.dataArr addObject:course];
                 }
             }
          
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self.collectionView reloadData];
             });
             NSLog(@"课程======列表返回数据 ： %@", dic);
             
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
}
    

#pragma mark -- UICollectionViewDelegateFlowLayout

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH - 20, 90);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CourseMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.course = (CourseModel *)_dataArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_playcourseblock) {
        CourseModel *course = (CourseModel *)_dataArr[indexPath.row];
        _playcourseblock(course);
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
